<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/2000/REC-xhtml1-20000126/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" />
  <meta http-equiv="expires" content="Fri, 26 Dec 2003 19:13:00 +0100" />
  <meta name="language" content="en" />
  <meta name="robots" content="index,follow" />
  <meta name="revisit-after" content="7 days" />
  <meta name="author" content="Dominique Stender, Paul Sullivan" />
  <meta name="date" content="2003-11-26T18:13:00+01:00?" />
  <meta name="description" content="Documentation for the CPAINT AJAX toolkit" />
  <meta name="keywords" content="AJAX, CPAINT, Asynchronous JavaScript and XML, XML, JavaScript, PHP, ASP, .NET, Perl" />
  <title>CPAINT documentation :: upgrade :: backend changes</title>
  <link rel="stylesheet" href="cpaint.css" media="screen" />
  <link rel="stylesheet" href="cpaint.print.css" media="print" />
  <script type="text/javascript" src="js/menu.js"></script>
  <style type="text/css">
<!--
.menu1 {font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 10px;
	text-decoration: none;
	font-weight:  bold;
}
-->
  </style>
</head>
<body>
  <p align="right">
    <br />
    <a href="http://sf.net/projects/cpaint/">
      <img src="gfx/css_logo.gif" alt="" border="0" height="51" width="182" alt=""  />
    </a>
  </p>
  <h1>CPAINT :: Cross-Platform Asynchronous INterface Toolkit</h1>
  <div id="menu">
    <h2>Contents</h2>
    <p>
      <script type="text/javascript">
      <!--
        generate_menu();
      //-->
      </script>
    </p>
    <br /><br /><br />
    <p>
      <img src="gfx/xhtml10.gif" alt="" border="0" height="15" width="80" /><br />
      <img src="gfx/css.gif" alt="" border="0" height="15" width="80" /><br />
      <a href="http://www.monorom.to/" target="_blank"><img src="gfx/monorom.gif" alt="monorom" border="0" height="15" width="80" /></a><br />
    <a rel="license" href="http://creativecommons.org/licenses/by/1.0/" target="_blank"><img src="gfx/cc.gif" alt="Creative Commons License" border="0" height="15" width="80" /></a></p>
    <p><br />
      <a href="http://creativecommons.org/licenses/by/1.0/" target="_blank" class="menu" rel="license">Project Services By <br />
      </a><a href="http://sourceforge.net/" target="_blank" class="menu1"><img src="http://sourceforge.net/sflogo.php?group_id=141041&amp;type=2" width="125" height="37" border="0" alt="SourceForge.net Logo"/></a> </p>
  </div>
  
<div id="content">
    <h2>Upgrade: <strong>
  Backend Changes</strong></h2>
    <p>Just like the frontend, the backend of CPAINT v2.x is completely object oriented. The first thing you will need to do is create a new instance of the <a href="backend.class.cpaint.html" class="code">cpaint</a> object and tell it to start executing code with the <a href="backend.class.cpaint.start.html" class="code">cpaint.start()</a> method.</p>
    <p>Here is an example in v1.x and it's equivalent in v2.x that returns data to the frontend using XML.</p>
    <p>v1.x</p>
    <blockquote>
      <p class="code">
        &lt;?php<br />
        &nbsp;&nbsp;require_once('cpaint.inc.php');<br /></p>
      <p class="code">
        &nbsp;&nbsp;function addNumbers($number1, $number2) {<br />
        &nbsp;&nbsp;&nbsp;&nbsp;cpaint_xml_open_result('response');<br />
        &nbsp;&nbsp;&nbsp;&nbsp;cpaint_xml_add_data</span>('answer', 'answer', ($number1 + $number2));<br>
        &nbsp;&nbsp;&nbsp;&nbsp;cpaint_xml_close_result</span>();<br />
        &nbsp;&nbsp;&nbsp;&nbsp;cpaint_xml_return_data</span>();        <br>
        &nbsp;&nbsp;}<br />
        ?&gt;
      </p>
    </blockquote>
    <p>v2.x</p>
    <blockquote>
      <p class="code">
        &lt;?php<br />
        &nbsp;&nbsp;require_once('cpaint2.inc.php');
      </p>
      <p class="code">
        &nbsp;&nbsp;$cp = new cpaint();<br />
        &nbsp;&nbsp;$cp->register('add');<br />
        &nbsp;&nbsp;$cp-&gt;start();<br />
        &nbsp;&nbsp;$cp-&gt;return_data();
      </p>
      <p class="code">
        &nbsp;&nbsp;function add($num1, $num2) {<br />
        &nbsp;&nbsp;&nbsp;&nbsp;global $cp;<br />
        &nbsp;&nbsp;&nbsp;&nbsp;$cp-&gt;set_attribute('id', 'response');<br />
        &nbsp;&nbsp;&nbsp;&nbsp;$cp-&gt;set_data($num1 + $num2);<br />
        &nbsp;&nbsp;}<br />
        ?&gt;
      </p>
    </blockquote>
    <p>As you can see, the new architecture makes it extremely easy to build XML responses, as it is naturally supported. <em>You must remember to put </em><span class="code">global $[cpaint_object];</span><em> in every function or the call will fail.</em></p>
    <p>If you aren't using XML, no problem. Here is a sample in both v1.x and the new method for v2.x:</p>
    <p>v1.x</p>
    <blockquote>
    <p class="code">
      &lt;?php<br />
      &nbsp;&nbsp;require_once(&quot;cpaint.inc.php&quot;);
    </p>
    <p class="code">
      &nbsp;&nbsp;function addNumbers($number1, $number2) {<br />
      &nbsp;&nbsp;&nbsp;&nbsp;return ($number1 + $number2);<br />
      &nbsp;&nbsp;}<br />
      ?&gt;
    </p>
  </blockquote>
    <p>v2.x</p>
    <blockquote>
      <p class="code">
        &lt;?php<br />
        &nbsp;&nbsp;require_once(&quot;cpaint2.inc.php&quot;);
      </p>
      <p class="code">
        &nbsp;&nbsp;$cp = new cpaint();<br />
        &nbsp;&nbsp;$cp->register('add');<br />
        &nbsp;&nbsp;$cp-&gt;start();<br />
        &nbsp;&nbsp;$cp-&gt;return_data();
      </p>
      <p class="code">
        &nbsp;&nbsp;function add($num1, $num2) {<br />
        &nbsp;&nbsp;&nbsp;&nbsp;global $cp;<br />
        &nbsp;&nbsp;&nbsp;&nbsp;$cp->set_data($num1 + $num2); <br />
        &nbsp;&nbsp;}<br />
        ?&gt;
      </p>
    </blockquote>
    <p>Notice - the only difference to the XML example is that I did not set an id attribute. However I <em>could</em> have set the id attribute, it wouldn't have made a difference.</p>
    <p>You see, there is absolutely <strong>no</strong> reason for you to change the backend code only because the frontend (which you might have no control over) desired a different output format.</p>
</div>
</body>
</html>
